#ifndef SPI_LCD_HPP
#define SPI_LCD_HPP

#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "../spi_display/SPI.hpp"
#include "lvgl.h"

/* 引脚定义 */
#define LCD_NUM_BL      GPIO_NUM_41
#define LCD_NUM_WR      GPIO_NUM_40
#define LCD_NUM_CS      GPIO_NUM_39
#define LCD_NUM_RST     GPIO_NUM_38


class LCDDisplay {
public:
    LCDDisplay();
    LCDDisplay(spi_host_device_t host, uint16_t wrPin, uint16_t csPin, uint16_t blPin, uint16_t rstPin);
    ~LCDDisplay();

    SPI controllerSPI;
    static LCDDisplay* instance;
    void init();
    void lcd_off();
    void lcd_hard_reset();
    void setSPIInterface(SPI spi_display);
    void lcdSetCursor(uint16_t xpos, uint16_t ypos);
    void lcdWriteCmd(uint8_t cmd);

    void lcdWriteData(const uint8_t *data, int len);
    void lcdDrawPixel(uint16_t xpos, uint16_t ypos, uint16_t color);
    void lcdPrintImage(uint16_t start_x, uint16_t end_x, uint16_t start_y, uint16_t end_y, uint16_t *data);
    void lcdSetWindow(uint16_t start_x, uint16_t end_x, uint16_t start_y, uint16_t end_y);
    static void lcdDisplayFlushcb(lv_display_t *disp_drv, const lv_area_t *area, uint8_t *px_map);
    void lcdClear(uint16_t color);
    void initScreen();
    void lcdOn();

private:
    spi_host_device_t _host;
    uint16_t _wrPin;
    uint16_t _csPin;
    uint16_t _blPin;
    uint16_t _rstPin;
    spi_device_handle_t *_spi_lcd_handle;
    gpio_config_t _gpio_init_struct;
    void _configGPIO(uint64_t pin_bit_mask, gpio_mode_t mode, gpio_pullup_t pull_up_en, gpio_pulldown_t pull_down_en, gpio_int_type_t intr_type);
};

#endif // SPI_LCD_HPP